//
//  HCFindViewController.m
//  HappyChildren
//
//  Created by WillyZhao on 2016/12/16.
//  Copyright © 2016年 WillyZhao. All rights reserved.
//

#import "HCFindViewController.h"
#import "HCFindContentView.h"
#import "HCFindModel.h"
#import "XFWkwebView.h"
#import <AFNetworking.h>
#import "HCAllUsedModel.h"
#import "HCEveryDayModel.h"

#import "YBKPPTVC.h"
#import "HCFindButtonsView.h"
#import "HotProjectView.h"
#import "HCFindHighPraiseView.h"
#import "HCFindExpectView.h"



@interface HCFindViewController ()<HCFindContentViewDelegate> {
    
    HCFindModel *model_find;
    HCFindContentView *findView;
    HCEveryDayModel *model_every;
    HCAllUsedModel *model_alluse;
    
    UIScrollView *contentScrollView;
    YBKPPTVC *pptvc;//banner图
    __weak  UIView *pptView;
}

@property (nonatomic, strong) HCFindButtonsView *buttonsView; //四个按钮
@property (nonatomic, strong) HotProjectView *hotProjectView;  //热门项目
@property (nonatomic, strong) HCFindExpectView *expectView; //积分兑换
@property (nonatomic, strong) HCFindHighPraiseView *highPraiseView; //大家好评
@property (nonatomic, strong) NSArray *naviNames;

@end

@implementation HCFindViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.textTitle = @"发现";
    
    //界面初始化
    [self loadSubViews];
    //数据请求协议
    self.dataRequest.delegate = self;
    //发送数据请求
    [self.dataRequest postHC_Find];
}

//数据请求回调
-(void)dataRequesterFinish:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveDictionary:(NSDictionary *)dictionary {

    if (aType == HC_EVERYDAY_TYPE) {
        
        //积分兑换
        if (dictionary.allKeys.count != 0) {
            
            self.expectView.expectArray = [dictionary objectForKey:@"goods"];
        }
        //获取大家好评数据
        [self.dataRequest postHC_ALLUSE];

    }else if (aType == HC_ALLUSE_TYPE) {
        
        //大家好评
        if (dictionary.allKeys.count != 0) {
            
            self.highPraiseView.highPraiseArray = [dictionary objectForKey:@"sp_offer"];
        }
        [self.dataRequest post_FindBannerImages];
        
    }else if (aType == HC_FIND){
        
        //热门项目
        if (dictionary.allKeys.count != 0) {
            
            self.hotProjectView.hotProjectArray = [dictionary objectForKey:@"f_index"];
        }
        //获取积分兑换数据
        [self.dataRequest postHC_EveryDay];
    }
    else if (aType == HC_FINDIMAGES_TYPE) {
    
        //banner图
        NSArray *ar = [NSArray arrayWithArray:dictionary[@"pic"]];
        pptvc.showModelArray = ar;
    }
}

//数据请求失败回调
-(void)dataRequesterFail:(DataRequester *)requester requesterWithType:(DataRequestType)aType receiveErrorDictionary:(NSDictionary *)dictionary {
    
    if (aType == HC_EVERYDAY_TYPE) {
        
        [self.dataRequest postHC_ALLUSE];
        
    }else if (aType == HC_ALLUSE_TYPE) {
        
        [self.dataRequest post_naviNames];
        
    }else if (aType == HC_FIND){
        
        [self.dataRequest postHC_EveryDay];
    }
    else if (aType == HC_NAVINAME_TYPE){
        
        [self.dataRequest post_FindBannerImages];
    }
    else if (aType == HC_FINDIMAGES_TYPE) {
        
    }
}

-(void)loadSubViews {
    
    
    CGRect frame = [self initBannerPic];
    self.buttonsView = [[HCFindButtonsView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(frame), Screen_width, YBKAdaptiveH(90))];
    [contentScrollView addSubview:self.buttonsView];
    
    __weak typeof (self)weakSelf = self;
    [weakSelf.buttonsView setClickFourButtonBlock:^(NSInteger index){
        
        switch (index) {
            case 0:
                //积分商城
            {
                [weakSelf pushChildrenViewController:@"HCGoodsScoreViewController" parameterObject:nil dataObject:nil];
            }
                break;
            case 1:
                //门店热榜
            {
                //            NSLog(@"门店热榜");
                [weakSelf pushChildrenViewController:@"HCHotStoreViewController" parameterObject:nil dataObject:nil];
            }
                break;
            case 2:
                //店家推荐
            {
                //            NSLog(@"店家推荐");
                [weakSelf pushChildrenViewController:@"HCStoreRecommendViewController" parameterObject:nil dataObject:nil];
            }
                break;
            case 3:
                //更多门店
            {
                NSLog(@"更多门店");
            }
                break;
            default:
                break;
        }
    }];
    
    self.hotProjectView = [[HotProjectView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.buttonsView.frame)+10, Screen_width, YBKAdaptiveH(125))];
    self.hotProjectView.backgroundColor = [UIColor whiteColor];
    [contentScrollView addSubview:self.hotProjectView];
    
    self.highPraiseView = [[HCFindHighPraiseView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.hotProjectView.frame), Screen_width, YBKAdaptiveH(126))];
    self.highPraiseView.backgroundColor = [UIColor whiteColor];
    [contentScrollView addSubview:self.highPraiseView];
    
    self.expectView = [[HCFindExpectView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.highPraiseView.frame), Screen_width, YBKAdaptiveW(146))];
    self.expectView.backgroundColor = [UIColor whiteColor];
    [contentScrollView addSubview:self.expectView];
    
    contentScrollView.contentSize = CGSizeMake(self.centerView.frame.size.width, CGRectGetMaxY(self.expectView.frame)+10);
    
    [weakSelf.expectView setDidClickCellBlock:^(NSString *url){
        
        [weakSelf pushChildrenViewController:@"HCFindDetailViewController" parameterObject:url dataObject:@"积分兑换"];
    }];
    [weakSelf.highPraiseView setDidClickCellBlock:^(NSString *url){
        
        [weakSelf pushChildrenViewController:@"HCFindDetailViewController" parameterObject:url dataObject:@"大家好评"];
    }];
    
    [weakSelf.hotProjectView setDidClickCellBlock:^(NSString *url){
        
        [weakSelf pushChildrenViewController:@"HCFindDetailViewController" parameterObject:url dataObject:@"热门项目"];
    }];
}

#pragma mark 初始化banner图片
-(CGRect)initBannerPic {
    
    contentScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.centerView.frame.size.width, self.centerView.frame.size.height)];
    //    contentScrollView.scrollEnabled = NO;
    contentScrollView.showsVerticalScrollIndicator = NO;
    [self.centerView addSubview:contentScrollView];
    
    pptvc = [[YBKPPTVC alloc] init];
    __weak YBKPPTVC *p= pptvc;
    //    __weak HCHomeViewController *ss = self;
    
    pptvc.clickBlock = ^(HCHomeBannerModel *model) {
        NSLog(@"点击了");
        //        p.pptPageControl.currentPage =
    };
    
    pptvc.scrollBlock = ^(NSInteger m) {
        NSLog(@"滑动");
        p.pptPageControl.currentPage = m;
    };
    
    //这里-20是一个bug
    pptvc.view.frame=CGRectMake(0, 0,self.centerView.bounds.size.width, YBKAdaptiveH(160));
    pptView=pptvc.view;
    pptvc.view.backgroundColor = [UIColor lightGrayColor];
    [self addChildViewController:pptvc];
    [pptvc didMoveToParentViewController:self];
    [contentScrollView addSubview:pptvc.view];
    return pptvc.view.frame;
}

-(void)HCFindContentViewClickedContent:(NSIndexPath *)indexPath view:(NSInteger)index {
    if (index == 1) {
//        NSLog(@"热推");
        HCFindData *data = model_find.f_index[indexPath.row];
        [self pushChildrenViewController:@"HCFindDetailViewController" parameterObject:data.url dataObject:@"热推"];
    }else if (index == 2) {
//        NSLog(@"每日特价");
        HCEveryDayData *data = model_every.all_used[indexPath.row];
        [self pushChildrenViewController:@"HCFindDetailViewController" parameterObject:data.url dataObject:@"每日特价"];
    }else if (index == 3) {
//        NSLog(@"大家都在用");
        HCAllUsedData *data = model_alluse.sp_offer[indexPath.row];
        [self pushChildrenViewController:@"HCFindDetailViewController" parameterObject:data.url dataObject:@"大家都在用"];
    }
}

-(void)HCFindContentViewClicked:(HCFindHeaderButton *)btn {
    switch (btn.tag) {
        case 1:
            //积分商城
        {
            [self pushChildrenViewController:@"HCScoreStoreViewController" parameterObject:nil dataObject:nil];
        }
            break;
        case 2:
            //门店热榜
        {
//            NSLog(@"门店热榜");
            [self pushChildrenViewController:@"HCHotStoreViewController" parameterObject:nil dataObject:nil];
        }
            break;
        case 3:
            //店家推荐
        {
//            NSLog(@"店家推荐");
            [self pushChildrenViewController:@"HCStoreRecommendViewController" parameterObject:nil dataObject:nil];
        }
            break;
        case 4:
            //更多门店
        {
            NSLog(@"更多门店");
        }
            break;
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
